import { Wechaty, WechatyBuilder, Message } from "wechaty";
import qrcode from "qrcode-terminal";
// import promptGpt from "./util/gpt";
const topics = ['测试']


class weChaty {
  bot: Wechaty  
  constructor() {
    this.bot =  WechatyBuilder.build();
    this.bot.on('scan', (code: string) => {
      qrcode.generate(code, { small: true });
    })
    this.bot.on('message', this.onMessage.bind(this));
  }
  async onMessage(message: Message) {
    const msgSelf = message.self() // 是否自己发给自己的消息
    if (msgSelf) return
    const contact = message.talker() // 发消息人
    const contactName = contact.name()
    const room = message.room() // 是否为群消息
    const content = message.text().trim()
    
    if (room)  {
      const topic = await room.topic()
      if (!topics.includes(topic)) return
      console.log(`群名: ${topic} 发消息人: ${contactName} 内容: ${content}`)
      if(content.substr(0, 4) == "gpt:" && content.substr(4)){
        // promptGpt(content.substr(4))
      } else {
        room.say(`发消息人: ${contactName} 内容: ${content} room`)
      }
      // message.say(`发消息人: ${contactName} 内容: ${content} message`)
    } else {
      // console.log(` 发消息人: ${contactName} 内容: ${content}`)
      // message.say(`发消息人: ${contactName} 内容: ${content}  message`)
      // contact.say(`发消息人: ${contactName} 内容: ${content} contact`)
    }
  }
  run() {
    this.bot.start();
  }
}
new weChaty().run();